{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Launch Videos\n",
    "Ther two designs below were used in the October video used to launch qiskit-metal on twitter.\n",
    "\n",
    "## First launch video\n",
    "\n",
    "### Preparations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import qiskit_metal as metal\n",
    "from qiskit_metal import designs, draw\n",
    "from qiskit_metal import MetalGUI, Dict, Headings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "design = designs.DesignPlanar()\n",
    "gui = MetalGUI(design)\n",
    "\n",
    "from qiskit_metal.qlibrary.qubits.transmon_pocket import TransmonPocket\n",
    "TransmonPocket.get_template_options(design)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set the default settings for trace width and trace gap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "design.variables['cpw_width'] = '15 um'\n",
    "design.variables['cpw_gap'] = '9 um'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create the design\n",
    "4 transmons (3 pins each) + 4 CPWs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Allow running the same cell here multiple times to overwrite changes\n",
    "design.overwrite_enabled = True\n",
    "\n",
    "## Custom options for all the transmons\n",
    "options = dict(\n",
    "    # Some options we want to modify from the defaults\n",
    "    # (see below for defaults)\n",
    "    pad_width = '425 um', \n",
    "    pocket_height = '650um',\n",
    "    # Adding 4 connectors (see below for defaults)\n",
    "    connection_pads=dict(\n",
    "        a = dict(loc_W=+1,loc_H=-1, pad_width='200um'),\n",
    "        b = dict(loc_W=-1,loc_H=+1, pad_height='30um'),\n",
    "        c = dict(loc_W=-1,loc_H=-1, pad_height='50um')\n",
    "    )\n",
    ")\n",
    "\n",
    "## Create 4 transmons\n",
    "\n",
    "q1 = TransmonPocket(design, 'Q1', options = dict(\n",
    "    pos_x='+2.42251mm', pos_y='+0.0mm', **options))\n",
    "q2 = TransmonPocket(design, 'Q2', options = dict(\n",
    "    pos_x='+0.0mm', pos_y='-0.95mm', orientation = '270', **options))\n",
    "q3 = TransmonPocket(design, 'Q3', options = dict(\n",
    "    pos_x='-2.42251mm', pos_y='+0.0mm', orientation = '180', **options))\n",
    "q4 = TransmonPocket(design, 'Q4', options = dict(\n",
    "    pos_x='+0.0mm', pos_y='+0.95mm', orientation = '90', **options))\n",
    "\n",
    "from qiskit_metal.qlibrary.tlines.meandered import RouteMeander\n",
    "RouteMeander.get_template_options(design)\n",
    "\n",
    "options = Dict(\n",
    "        lead=Dict(\n",
    "            start_straight='0.2mm',\n",
    "            end_straight='0.2mm'),\n",
    "        trace_gap='9um',\n",
    "        trace_width='15um')\n",
    "\n",
    "def connect(component_name: str, component1: str, pin1: str, component2: str, pin2: str,\n",
    "            length: str, asymmetry='0 um', flip=False, fillet='90um'):\n",
    "    \"\"\"Connect two pins with a CPW.\"\"\"\n",
    "    myoptions = Dict(\n",
    "        fillet=fillet,\n",
    "        pin_inputs=Dict(\n",
    "            start_pin=Dict(\n",
    "                component=component1,\n",
    "                pin=pin1),\n",
    "            end_pin=Dict(\n",
    "                component=component2,\n",
    "                pin=pin2)),\n",
    "        total_length=length)\n",
    "    myoptions.update(options)\n",
    "    myoptions.meander.asymmetry = asymmetry\n",
    "    myoptions.meander.lead_direction_inverted = 'true' if flip else 'false'\n",
    "    return RouteMeander(design, component_name, myoptions)\n",
    "\n",
    "asym = 140\n",
    "cpw1 = connect('cpw1', 'Q1', 'c', 'Q2', 'b', '5.6 mm', f'+{asym}um')\n",
    "cpw2 = connect('cpw2', 'Q3', 'b', 'Q2', 'c', '5.7 mm', f'-{asym}um', flip=True)\n",
    "cpw3 = connect('cpw3', 'Q3', 'c', 'Q4', 'b', '5.6 mm', f'+{asym}um')\n",
    "cpw4 = connect('cpw4', 'Q1', 'b', 'Q4', 'c', '5.7 mm', f'-{asym}um', flip=True)\n",
    "\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4 Open to Ground Pins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit_metal.qlibrary.terminations.open_to_ground import OpenToGround\n",
    "\n",
    "otg1 = OpenToGround(design, 'OTG1', options = dict(pos_x='2771um', pos_y='0', orientation='180', gap='9um', width='15um'))\n",
    "otg2 = OpenToGround(design, 'OTG2', options = dict(pos_x='0um', pos_y='-1301um', orientation='90', gap='9um', width='15um'))\n",
    "otg3 = OpenToGround(design, 'OTG3', options = dict(pos_x='-2771um', pos_y='0', orientation='0', gap='9um', width='15um'))\n",
    "otg4 = OpenToGround(design, 'OTG4', options = dict(pos_x='0um', pos_y='1301um', orientation='270', gap='9um', width='15um'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "V1 Launchers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit_metal.qlibrary.terminations.launchpad_wb import LaunchpadWirebond\n",
    "\n",
    "p1_c = LaunchpadWirebond(design, 'P1_C', options = dict(pos_x='4000um', pos_y='2812um', orientation='270', lead_length='0um', cpw_gap='9um', cpw_width='15um'))\n",
    "p2_c = LaunchpadWirebond(design, 'P2_C', options = dict(pos_x='4000um', pos_y='-2812um', orientation='90', lead_length='0um', cpw_gap='9um', cpw_width='15um'))\n",
    "p3_c = LaunchpadWirebond(design, 'P3_C', options = dict(pos_x='-4000um', pos_y='-2812um', orientation='90', lead_length='0um', cpw_gap='9um', cpw_width='15um'))\n",
    "p4_c = LaunchpadWirebond(design, 'P4_C', options = dict(pos_x='-4000um', pos_y='2812um', orientation='270', lead_length='0um', cpw_gap='9um', cpw_width='15um'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "V2 Launchers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit_metal.qlibrary.terminations.launchpad_wb_coupled import LaunchpadWirebondCoupled\n",
    "\n",
    "p1_q = LaunchpadWirebondCoupled(design, 'P1_Q', options = dict(pos_x='4020um', pos_y='0', orientation='180', lead_length='0um', cpw_gap='9um', cpw_width='15um'))\n",
    "p2_q = LaunchpadWirebondCoupled(design, 'P2_Q', options = dict(pos_x='-990um', pos_y='-2812um', orientation='90', lead_length='0um', cpw_gap='9um', cpw_width='15um'))\n",
    "p3_q = LaunchpadWirebondCoupled(design, 'P3_Q', options = dict(pos_x='-4020um', pos_y='0', orientation='0', lead_length='0um', cpw_gap='9um', cpw_width='15um'))\n",
    "p4_q = LaunchpadWirebondCoupled(design, 'P4_Q', options = dict(pos_x='990um', pos_y='2812um', orientation='270', lead_length='0um', cpw_gap='9um', cpw_width='15um'))\n",
    "\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Charge Lines to Corners"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from collections import OrderedDict\n",
    "\n",
    "from qiskit_metal.qlibrary.tlines.anchored_path import RouteAnchors\n",
    "\n",
    "anchors1c = OrderedDict()\n",
    "anchors1c[0] = np.array([3, 0])\n",
    "anchors1c[1] = np.array([3, 2.5]) \n",
    "anchors1c[2] = np.array([4, 2.5])\n",
    "\n",
    "options_line_cl1 = {'pin_inputs': \n",
    "            {'start_pin': {'component': 'OTG1', 'pin': 'open'}, \n",
    "             'end_pin': {'component': 'P1_C', 'pin': 'tie'}},\n",
    "            'leadin': {'start': '0um', 'end': '0um'},\n",
    "            'anchors': anchors1c,\n",
    "            'trace_gap': '9um',\n",
    "            'trace_width': '15um',\n",
    "            'fillet': '90um'        \n",
    "            }\n",
    "\n",
    "cl1 = RouteAnchors(design, 'line_cl1', options_line_cl1)\n",
    "\n",
    "anchors2c = OrderedDict()\n",
    "anchors2c[0] = np.array([0, -1.5])\n",
    "anchors2c[1] = np.array([2, -1.5]) \n",
    "anchors2c[2] = np.array([2, -2.5])\n",
    "anchors2c[3] = np.array([4, -2.5])\n",
    "\n",
    "options_line_cl2 = {'pin_inputs': \n",
    "            {'start_pin': {'component': 'OTG2', 'pin': 'open'}, \n",
    "             'end_pin': {'component': 'P2_C', 'pin': 'tie'}},\n",
    "            'leadin': {'start': '0um', 'end': '0um'},\n",
    "            'anchors': anchors2c,\n",
    "            'fillet': '90um',\n",
    "            'trace_gap': '9um',\n",
    "            'trace_width': '15um'\n",
    "            }\n",
    "\n",
    "cl2 = RouteAnchors(design, 'line_cl2', options_line_cl2)\n",
    "\n",
    "anchors3c = OrderedDict()\n",
    "anchors3c[0] = np.array([-3, 0])\n",
    "anchors3c[1] = np.array([-3, -2.5]) \n",
    "anchors3c[2] = np.array([-4, -2.5])\n",
    "\n",
    "options_line_cl3 = {'pin_inputs': \n",
    "            {'start_pin': {'component': 'OTG3', 'pin': 'open'}, \n",
    "             'end_pin': {'component': 'P3_C', 'pin': 'tie'}},\n",
    "            'leadin': {'start': '0um', 'end': '0um'},\n",
    "            'anchors': anchors3c,\n",
    "            'fillet': '90um',\n",
    "            'trace_gap': '9um',\n",
    "            'trace_width': '15um'\n",
    "            }\n",
    "\n",
    "cl3 = RouteAnchors(design, 'line_cl3', options_line_cl3)\n",
    "\n",
    "anchors4c = OrderedDict()\n",
    "anchors4c[0] = np.array([0, 1.5])\n",
    "anchors4c[1] = np.array([-2, 1.5]) \n",
    "anchors4c[2] = np.array([-2, 2.5])\n",
    "anchors4c[3] = np.array([-4, 2.5])\n",
    "\n",
    "options_line_cl4 = {'pin_inputs': \n",
    "            {'start_pin': {'component': 'OTG4', 'pin': 'open'}, \n",
    "             'end_pin': {'component': 'P4_C', 'pin': 'tie'}},\n",
    "            'leadin': {'start': '0um', 'end': '0um'},\n",
    "            'anchors': anchors4c,\n",
    "            'fillet': '90um',\n",
    "            'trace_gap': '9um',\n",
    "            'trace_width': '15um'\n",
    "            }\n",
    "\n",
    "cl4 = RouteAnchors(design, 'line_cl4', options_line_cl4)\n",
    "\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exchange Coupler Lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "options = Dict(\n",
    "        lead=Dict(\n",
    "            start_straight='0.35mm',\n",
    "            end_straight='0.15mm'),\n",
    "        trace_gap='9um',\n",
    "        trace_width='15um')\n",
    "\n",
    "ol1 = connect('ol1', 'Q1', 'a', 'P1_Q', 'tie', '5.5 mm', f'+{asym}um', flip=True)\n",
    "ol2 = connect('ol2', 'Q2', 'a', 'P2_Q', 'tie', '13.0 mm', f'+{asym}um', flip=True)\n",
    "ol3 = connect('ol3', 'Q3', 'a', 'P3_Q', 'tie', '5.5 mm', f'+{asym}um', flip=True)\n",
    "ol4 = connect('ol4', 'Q4', 'a', 'P4_Q', 'tie', '13.0 mm', f'+{asym}um', flip=True)\n",
    "\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Export\n",
    "We now demonstrate the export to GDS. QDesign enables GDS renderer during init."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_gds = design.renderers.gds\n",
    "a_gds.options"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you have a fillet_value, this will not be applied to the LineSegments that are shorter than 2*fillet_value. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_gds.options['short_segments_to_not_fillet'] = 'True'\n",
    "a_gds.options['check_short_segments_by_scaling_fillet'] = 2.0\n",
    "a_gds.options['path_filename'] = '../resources/Fake_Junctions.GDS'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Export GDS file for all components in design.\n",
    "Please include the path to the location where you want to eport the GDS file. No path, will export to the folder where this notebook resides.\n",
    "\n",
    "`export_to_gds(self, file_name: str, highlight_qcomponents: list = []) -> int`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_gds.export_to_gds(\"Launch_Notebook.gds\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gui.main_window.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### END of first video\n",
    "\n",
    "## Second launch video\n",
    "Requires both Ansys and the design from the previous setup\n",
    "\n",
    "### Preparations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import qiskit_metal as metal\n",
    "from qiskit_metal import designs, draw\n",
    "from qiskit_metal import MetalGUI, Dict, Headings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit_metal.renderers.renderer_ansys.ansys_renderer import QAnsysRenderer\n",
    "\n",
    "QAnsysRenderer.default_options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "design = designs.DesignPlanar()\n",
    "gui = MetalGUI(design)\n",
    "\n",
    "from qiskit_metal.qlibrary.qubits.transmon_pocket import TransmonPocket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "design.variables['cpw_width'] = '15 um'\n",
    "design.variables['cpw_gap'] = '9 um'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create the design"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Allow running the same cell here multiple times to overwrite changes\n",
    "design.overwrite_enabled = True\n",
    "\n",
    "## Custom options for all the transmons\n",
    "options = dict(\n",
    "    # Some options we want to modify from the defaults\n",
    "    # (see below for defaults)\n",
    "    pad_width = '425 um', \n",
    "    pocket_height = '650um',\n",
    "    # Adding 4 connectors (see below for defaults)\n",
    "    connection_pads=dict(\n",
    "        a = dict(loc_W=+1,loc_H=-1, pad_width='200um'),\n",
    "        b = dict(loc_W=-1,loc_H=+1, pad_height='30um'),\n",
    "        c = dict(loc_W=-1,loc_H=-1, pad_height='50um')\n",
    "    )\n",
    ")\n",
    "\n",
    "## Create 4 transmons\n",
    "\n",
    "q1 = TransmonPocket(design, 'Q1', options = dict(\n",
    "    pos_x='+2.42251mm', pos_y='+0.0mm', **options))\n",
    "q2 = TransmonPocket(design, 'Q2', options = dict(\n",
    "    pos_x='+0.0mm', pos_y='-0.95mm', orientation = '270', **options))\n",
    "q3 = TransmonPocket(design, 'Q3', options = dict(\n",
    "    pos_x='-2.42251mm', pos_y='+0.0mm', orientation = '180', **options))\n",
    "q4 = TransmonPocket(design, 'Q4', options = dict(\n",
    "    pos_x='+0.0mm', pos_y='+0.95mm', orientation = '90', **options))\n",
    "\n",
    "from qiskit_metal.qlibrary.tlines.meandered import RouteMeander\n",
    "RouteMeander.get_template_options(design)\n",
    "\n",
    "options = Dict(\n",
    "        lead=Dict(\n",
    "            start_straight='0.2mm',\n",
    "            end_straight='0.2mm'),\n",
    "        trace_gap='9um',\n",
    "        trace_width='15um')\n",
    "\n",
    "def connect(component_name: str, component1: str, pin1: str, component2: str, pin2: str,\n",
    "            length: str, asymmetry='0 um', flip=False, fillet='90um'):\n",
    "    \"\"\"Connect two pins with a CPW.\"\"\"\n",
    "    myoptions = Dict(\n",
    "        fillet=fillet,\n",
    "        pin_inputs=Dict(\n",
    "            start_pin=Dict(\n",
    "                component=component1,\n",
    "                pin=pin1),\n",
    "            end_pin=Dict(\n",
    "                component=component2,\n",
    "                pin=pin2)),\n",
    "        total_length=length)\n",
    "    myoptions.update(options)\n",
    "    myoptions.meander.asymmetry = asymmetry\n",
    "    myoptions.meander.lead_direction_inverted = 'true' if flip else 'false'\n",
    "    return RouteMeander(design, component_name, myoptions)\n",
    "\n",
    "asym = 140\n",
    "cpw1 = connect('cpw1', 'Q1', 'c', 'Q2', 'b', '5.6 mm', f'+{asym}um')\n",
    "cpw2 = connect('cpw2', 'Q3', 'b', 'Q2', 'c', '5.7 mm', f'-{asym}um', flip=True)\n",
    "cpw3 = connect('cpw3', 'Q3', 'c', 'Q4', 'b', '5.6 mm', f'+{asym}um')\n",
    "cpw4 = connect('cpw4', 'Q1', 'b', 'Q4', 'c', '5.7 mm', f'-{asym}um', flip=True)\n",
    "\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Render the design\n",
    "This section directly interacts with the renderer, thus will not allow analysis. For analyzing the design, please refer to the method described in the `tutorial notebook 4.2`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fourq = design.renderers.hfss"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before proceding, open Ansys Electronic Desktop manually and wait for it to completely open. Also create a project if it does not get created automatically.\n",
    "\n",
    "The following cell will error out if Ansys is not configured correctly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fourq.connect_ansys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fourq.add_eigenmode_design(\"FourQ\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Render everything from the design onto Ansys."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fourq.render_design([], [])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fourq.disconnect_ansys()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
